package com.pipayshop.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.pipayshop.domain.ShopCommodityHistory;
import com.pipayshop.domain.vo.ShopCommodityCollectionVo;
import com.pipayshop.domain.vo.ShopCommodityListVo;
import com.pipayshop.domain.vo.ShopCommodityMinVo;
import com.pipayshop.entity.vo.PageDataVO;
import com.pipayshop.mapper.OnlineStoreShopCommodityHistoryMapper;
import com.pipayshop.service.OnlineStoreShopCommodityHistoryService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author xiaoqiu
 * @date 2024/9/23 10:33
 * @description
 */
@Service
public class OnlineStoreShopCommodityHistoryServiceImpl extends ServiceImpl<OnlineStoreShopCommodityHistoryMapper, ShopCommodityHistory> implements OnlineStoreShopCommodityHistoryService {

    @Autowired
    private OnlineStoreShopCommodityHistoryMapper shopCommodityHistoryMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addHistory(ShopCommodityHistory shopCommodityHistory) {
        if (shopCommodityHistory == null || shopCommodityHistory.getCommodityId() == null || shopCommodityHistory.getUserId() == null) {
            log.error("参数不能为空");
            return false;
        }
        ShopCommodityHistory histories = shopCommodityHistoryMapper.selectOne(new LambdaQueryWrapper<ShopCommodityHistory>()
                .eq(ShopCommodityHistory::getCommodityId, shopCommodityHistory.getCommodityId())
                .eq(ShopCommodityHistory::getUserId, shopCommodityHistory.getUserId()));
        int result;
        shopCommodityHistory.setCreateTime(new Date());
        if (histories == null) {
            result = shopCommodityHistoryMapper.insert(shopCommodityHistory);
        } else {
            result = shopCommodityHistoryMapper.update(null, new LambdaUpdateWrapper<ShopCommodityHistory>()
                    .eq(ShopCommodityHistory::getCommodityId, shopCommodityHistory.getCommodityId())
                    .eq(ShopCommodityHistory::getUserId, shopCommodityHistory.getUserId())
                    .set(ShopCommodityHistory::getCreateTime, new Date()));
        }
        return result > 0;

    }

    /**
     * 删除用户浏览网店商品的历史记录
     * @param userId 用户id
     * @param commodityId 商品id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteHistory(String userId, String commodityId) {
            return shopCommodityHistoryMapper.delete(new LambdaQueryWrapper<ShopCommodityHistory>()
                    .eq(StrUtil.isNotEmpty(commodityId),ShopCommodityHistory::getCommodityId, commodityId)
                    .eq(StrUtil.isNotEmpty(userId),ShopCommodityHistory::getUserId,userId)) > 0;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean orderDeleteHistory() {
        shopCommodityHistoryMapper.orderDeleteHistory();
        return true;
    }

//    @Override
//    public List<ShopCommodityMinVo> getHistory(String userId) {
//        return shopCommodityHistoryMapper.getHistory(userId);
//
//    }

    /**
     * 根据用户id浏览网店商品的历史记录
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageDataVO getHistory(Integer page, Integer limit, String userId) {
        Integer integer = shopCommodityHistoryMapper.selectAllHistoryProductByUserId(userId);
        List<ShopCommodityListVo> shopCommodityCollectionVos = shopCommodityHistoryMapper.getHistory((page - 1) * limit, limit, userId);
        return new PageDataVO(integer,shopCommodityCollectionVos);
    }


}
